package iscript.coreFramework.data 
{
	import flash.utils.Dictionary;
	import iscript.coreFramework.util.DictionaryUtil;

	public class HashTable 
	{
		
		private var _data:Vector.<HashTableItem> = new Vector.<HashTableItem>();
		public function HashTable() 
		{
			 
		}
		
		
		
		public var readOnly:Boolean;
		
	    public function add(key:*, value:*):Boolean {
		   if (!containKey(key)) {
			      var  item:HashTableItem = new HashTableItem();
				    item.key = key;
					item.value = value;
					_data.push(item);
					return true;
			   }
			   
			 return false;
		}
		
        public function getValue(key:*):* {
			 for each ( var item:HashTableItem  in _data) {
				     if (item.key == key)
					 return item.value 
				 }
			return null;
			}
		 
			
		public function updateValue(key:*, value:*):void {
			    if (readOnly) throw Error("it's  readonly");
				LOOPLoigc:
			    for each ( var item:HashTableItem  in _data) {
					 if (item.key == key)
					   {
						   item.value = value;
					       break LOOPLoigc;
					    }
					}
			}	
			
		/**
		 * 根据value获得多个Key
		 * @param	value
		 * @return
		 */	
		public function getKeysByVaule(value:*):Array {
			  var data:Array = new Array();
			  for each ( var item:HashTableItem  in _data) {
					 if (item.value == value){
					     data.push(item.key);
					  }
					}
			  return data;
			}	
			
		/**
		 * 根据value 获得唯一的key
		 * @param	value
		 * @return
		 */	
		public function getUniqueKey(value:*):*{
			   for each ( var item:HashTableItem  in _data) {
					 if (item.value == value){
					     return item.key;
					  }
					}
				return null;
			}	
			
		public function getAllKeys():Array {				
		    var keys:Array = new Array();
			for each ( var item:HashTableItem  in _data) {
					   keys.push(item.key)
					}
			return keys;
		}
		
		public function getAllValues():Array {
			 var values:Array = new Array();
				for each ( var item:HashTableItem  in _data) {
						   values.push(item.value)
						}
				return values;
			}
		 
			
			
		public function getItemByIndex(index:int):HashTableItem {
			  return _data[index];
			}	
			
		public function getItemByKey(key:*):HashTableItem {
			  return  getItemByIndex(getIndexByKey(key));
			}	
               
		public function getIndexByKey(key:*):int {
			   for (var i:int = 0; i <_data.length ; i++) 
			   {
				   if ( _data[i].key == key) {
					      return i;
					   }
			   }
			   return -1;
			}
			
			
		public function removeByKey(key:*):void {
			  LOOP:
			  for (var i:int = 0; i <_data.length; i++) 
			  {
				  if (_data[i].key == key){ 
				      _data.splice(i, 1);
				      break LOOP;
					}
			  }
			}
		 
		public function removeByValue(value:*):void {
			 var index:Vector.<int> = new Vector.<int>();
			 for (var i:int = 0; i <_data.length; i++) 
			  {
				  if (_data[i].value == value) 
				  index.push(i);
			  }
			  index.reverse();
			  
			  for (var j:int = 0; j <index.length; j++) 
			  {
				   _data.splice(index[j], 1);
			  }
			}	
			
		public function containKey(key:*):Boolean {
			    for each ( var item:HashTableItem  in _data) {
					 if (item.key == key) return true;
					}
				return false;
			}
		
		
		public function get size():int{
				return _data.length;
		}

		public function isEmpty():Boolean{
				return size == 0;
		}
		
		public function reverse():void {
			_data.reverse();
			}
		 
		public function get data():Vector.<HashTableItem> { return _data; }
			
		public function clear():void {
			_data = new Vector.<HashTableItem>();
			}
			
	}
}


internal class HashTableItem {
	 public var  key:*;
	 public var  value:*;
	}
